package com.campus.activity.controller;

import com.campus.activity.common.Result;
import com.campus.activity.entity.Activity;
import com.campus.activity.entity.Student;
import com.campus.activity.service.ActivityService;
import com.campus.activity.service.StudentService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 学生管理控制器
 * 
 * @author System
 * @since 2024
 */
@RestController
@RequestMapping("/students")
@Tag(name = "学生管理", description = "学生信息的增删改查管理")
@Validated
@CrossOrigin(origins = "*")
public class StudentController {

    @Autowired
    private StudentService studentService;
    
    @Autowired
    private ActivityService activityService;

    @PostMapping
    @Operation(summary = "添加学生", description = "创建新的学生信息")
    public Result<Boolean> addStudent(@Valid @RequestBody Student student) {
        boolean success = studentService.addStudent(student);
        if (success) {
            return Result.success("学生添加成功", true);
        } else {
            return Result.error("学生添加失败，学号可能已存在");
        }
    }

    @PutMapping("/{studentId}")
    @Operation(summary = "更新学生信息", description = "更新指定学生的信息")
    public Result<Boolean> updateStudent(
            @Parameter(description = "学生ID") @PathVariable Integer studentId,
            @Valid @RequestBody Student student) {
        boolean success = studentService.updateStudent(studentId, student);
        return success ? Result.success("学生信息更新成功", true) : Result.error("学生信息更新失败");
    }

    @DeleteMapping("/{studentId}")
    @Operation(summary = "删除学生", description = "删除指定的学生")
    public Result<Boolean> deleteStudent(
            @Parameter(description = "学生ID") @PathVariable Integer studentId) {
        boolean success = studentService.deleteStudent(studentId);
        return success ? Result.success("学生删除成功", true) : Result.error("学生删除失败");
    }

    @GetMapping("/{studentId}")
    @Operation(summary = "获取学生详情", description = "根据学生ID获取学生详细信息")
    public Result<Student> getStudent(
            @Parameter(description = "学生ID") @PathVariable Integer studentId) {
        Student student = studentService.getById(studentId);
        return student != null ? Result.success(student) : Result.notFound();
    }

    @GetMapping("/number/{studentNumber}")
    @Operation(summary = "根据学号查询学生", description = "根据学号获取学生信息")
    public Result<Student> getStudentByNumber(
            @Parameter(description = "学号") @PathVariable String studentNumber) {
        Student student = studentService.getStudentByNumber(studentNumber);
        return student != null ? Result.success(student) : Result.notFound();
    }

    @GetMapping("/name/{studentName}")
    @Operation(summary = "根据姓名查询学生", description = "根据学生姓名模糊查询学生列表")
    public Result<List<Student>> getStudentsByName(
            @Parameter(description = "学生姓名") @PathVariable String studentName) {
        List<Student> students = studentService.getStudentsByName(studentName);
        return Result.success(students);
    }

    @GetMapping
    @Operation(summary = "获取所有学生", description = "获取所有学生列表")
    public Result<List<Student>> getAllStudents() {
        List<Student> students = studentService.getAllStudents();
        return Result.success(students);
    }

    @GetMapping("/check-number")
    @Operation(summary = "检查学号", description = "检查学号是否已存在")
    public Result<Boolean> checkStudentNumber(
            @Parameter(description = "学号") @RequestParam String studentNumber) {
        boolean exists = studentService.isStudentNumberExists(studentNumber);
        return Result.success(exists);
    }

    @GetMapping("/dashboard/stats")
    @Operation(summary = "获取学生仪表板统计", description = "获取学生仪表板的统计信息")
    public Result<Map<String, Object>> getDashboardStats(
            @Parameter(description = "学生ID") @RequestParam Integer studentId) {
        Map<String, Object> stats = new HashMap<>();
        
        // 获取活动统计
        List<Activity> allActivities = activityService.getAllOpenActivities();
        long totalActivities = allActivities.size();
        
        // 这里应该从注册表中获取学生的注册统计，暂时使用模拟数据
        stats.put("totalActivities", totalActivities);
        stats.put("registeredActivities", 0); // 需要从注册服务获取
        stats.put("completedActivities", 0); // 需要从注册服务获取
        stats.put("upcomingActivities", 0); // 需要从注册服务获取
        
        return Result.success(stats);
    }

    @GetMapping("/activities/recommended")
    @Operation(summary = "获取推荐活动", description = "获取为学生推荐的活动")
    public Result<List<Activity>> getRecommendedActivities(
            @Parameter(description = "学生ID") @RequestParam Integer studentId) {
        // 暂时返回热门活动作为推荐
        List<Activity> activities = activityService.getPopularActivities();
        return Result.success(activities);
    }

    @GetMapping("/activities/available")
    @Operation(summary = "获取可报名活动", description = "获取学生可以报名的活动")
    public Result<List<Activity>> getAvailableActivities() {
        List<Activity> activities = activityService.getAllOpenActivities();
        return Result.success(activities);
    }
}